package com.atguigu.ggkt.exception;

import com.atguigu.ggkt.result.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2022/6/29 14:28
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result error(Exception ex) {
        return Result.fail().message("服务器异常: " + ex.getMessage());
    }

    /**
     * 特定异常处理
     * @param ex
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    public Result error(ArithmeticException ex) {
        return Result.fail().message("异常: " + ex.getMessage());
    }

    /**
     * 特定异常处理
     * @param ex
     * @return
     */
    @ExceptionHandler(GgktException.class)
    public Result error(GgktException ex) {
        return Result.fail().code(ex.getCode()).message(ex.getMsg());
    }
}
